package projectmanager

import org.springframework.dao.DataIntegrityViolationException

class UserController {

def scaffold=true

def index() {}

def login = {

}

def authenticate = {
		 def user = User.findByUserNameAndPassword(params.userName, params.password)
		 if(user){
			 session.user = user
			 flash.message = "Hello ${user.firstName} ${user.lastName}"
			 switch (user.role){
			 case 'Admin':
			   redirect(uri: "/admin")
			   break
			 case 'Manager':
			  redirect(uri: "/manager")
			  break
			 case 'User':
			  redirect(uri:"/userPage")
			  break
			 default:
			 redirect(uri:"/")
			 }
			
		 }else{
		 flash.message = "Sorry, ${params.userName} Please try again."
		 redirect(action:"login")
		 }
	 }
def logout = {
flash.message = "Goodbye ${session.user.firstName} ${session.user.lastName}"
redirect(uri: "/")
session.user = null
}

List projects() {
return member.collect{it.projects}
}

List addToProjects(Project projects) {
Membership.link(this, projects)
return projects()
}

List removeFromProjects(Project projects) {
Membership.unlink(this, projects)
return projects()
}

}

